package com.yztstudy.Service.Impl;

import com.yztstudy.Service.AdminCategoryService;
import com.yztstudy.clients.CategoryFeignClient;
import com.yztstudy.entity.Category;
import com.yztstudy.param.PageParam;
import com.yztstudy.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Resource
    private CategoryFeignClient categoryFeignClient;

    /**
     * TODO 分页展示类别
     * @param pageParam 分页参数
     * @return
     */
    @Cacheable(value = "list.category", key = "#pageParam.currentPage + '-' + #pageParam.pageSize", cacheManager = "cacheManagerHour")
    @Override
    public R listPage(PageParam pageParam) {

        // TODO 1.直接远程调用类别服务
        R result = categoryFeignClient.listPage(pageParam);

        // TODO 2.日志记录
        if (result.getCode().equals(R.SUCCESS_CODE))
        {
            log.info("AdminUserServiceImpl.listPage方法执行完毕,结果为:{}", "类别分页查询成功!");
        }else {
            log.info("AdminUserServiceImpl.listPage方法执行完毕,结果为:{}", "类别分页查询失败!");
        }
        return result;
    }


    /**
     * TODO 新增类别
     * @param category 类别对象
     * @return
     */
    @CacheEvict(value = "list.category", allEntries = true) // 清空缓存
    @Override
    public R saveCategory(Category category) {

        // TODO 1.直接远程调用类别服务
        R result = categoryFeignClient.saveCategory(category);

        // TODO 2.日志记录
        if (result.getCode().equals(R.SUCCESS_CODE))
        {
            log.info("AdminUserServiceImpl.saveCategory方法执行完毕,结果为:{}", "类别新增成功!");
        }else {
            log.info("AdminUserServiceImpl.saveCategory方法执行完毕,结果为:{}", "类别新增失败!");
        }
        return result;
    }


    /**
     * TODO 删除类别
     * @param categoryId 类别Id
     * @return
     */
    @CacheEvict(value = "list.category", allEntries = true) // 清空缓存
    @Override
    public R removeCategory(Integer categoryId) {

        // TODO 1.直接远程调用类别删除
        R result = categoryFeignClient.adminRemove(categoryId);

        // TODO 2.日志记录
        if (result.getCode().equals(R.SUCCESS_CODE))
        {
            log.info("AdminUserServiceImpl.removeCategory方法执行完毕,结果为:{}", "类别删除成功!");
        }else {
            log.info("AdminUserServiceImpl.removeCategory方法执行完毕,结果为:{}", "类别删除失败!");
        }
        return result;
    }


    /**
     * TODO 更新类别
     * @param category 类别对象
     * @return
     */
    @CacheEvict(value = "list.category", allEntries = true) // 清空缓存
    @Override
    public R updateCategory(Category category) {

        // TODO 1.直接远程调用类别更新
        R result = categoryFeignClient.adminUpdate(category);

        // TODO 2.日志记录
        if (result.getCode().equals(R.SUCCESS_CODE))
        {
            log.info("AdminUserServiceImpl.updateCategory方法执行完毕,结果为:{}", "类别更新成功!");
        }else {
            log.info("AdminUserServiceImpl.updateCategory方法执行完毕,结果为:{}", "类别更新失败!");
        }
        return result;
    }
}
